package net.looker.data;

import net.looker.config.CategoryConfig;
import net.looker.config.SiteConfig;
import net.looker.util.URLUtils;

/**
 * 一个栏目
 * @author solosky
 *
 */

public class Category 
{

	private CategoryConfig categoryConfig;
	private SiteConfig     siteConfig;
	
	public Category(CategoryConfig categoryConfig,SiteConfig siteConfig)
	{
		this.categoryConfig  = categoryConfig; 
		this.siteConfig      = siteConfig;
	}
	/**
	 * 返回栏目首页地址
	 * @return
	 */
	public  String getIndex()
	{
		return siteConfig.get("site.url")+siteConfig.get("list.index").replace("{categoryid}", categoryConfig.get("id"));
	}
	/**
	 * 返回指定页码的栏目地址
	 * @param page
	 * @return
	 */
	public  String getURL(int page)
	{
		String url = siteConfig.get("site.url")+siteConfig.get("list.url");
		url = url.replace("{categoryid}", categoryConfig.get("id"));
		
		String paging = siteConfig.get("list.paging");
		if(paging.equals("offset"))
			return url.replace("{page}",Integer.toString(page-1));
		else
			return url.replace("{page}",Integer.toString(page));
	}
	
	/**
	 * 返回栏目标题
	 * @return
	 */
	public String getTitle()
	{
		return  categoryConfig.get("title");
	}
	
	/**
	 * 返回栏目编号
	 * @return
	 */
	public String getId()
	{
		return  categoryConfig.get("id");
	}
	
	/**
	 * 返回栏目监视器更新间隔
	 * @return
	 */
	public long getMonitorInterval()
	{
		return Long.parseLong(categoryConfig.get("monitor.interval"))*60*1000;
	}
	
	
	/**
	 * 返回更新器更新间隔
	 * @return
	 */
	public long getUpdaterInterval()
	{
		return Long.parseLong(categoryConfig.get("updater.interval"))*60*1000;
	}
	
	/**
	 * 返回栏目HASH
	 * @return
	 */
	public String getCategoryHash()
	{
		return URLUtils.hash2(getIndex());
	}
	
	
	public int getUpdaterNewsCnt()
	{
		return Integer.parseInt((categoryConfig.get("updater.newsCnt")));
	}
	
	public SiteConfig getSiteConfig()
	{
		return siteConfig;
	}

}
